//
// Created by sukai on 22-9-19.
//

#include <iostream>
#include <ros/ros.h>
#include <signal.h>
#include "contnav_msgs/Dictionaries.h"
#include "contnav_msgs/DictionariesVector.h"
#include "contnav_srvs/TABLE_CONTORLLER.h"
#include "common.h"

using namespace std;
/**
 *
 * @param argc
 * @param argv
 * @return
 * 测试节点
 * 2022-09-19
 * sukai
 * rosrun contnav testMysql_node
 */



//数据库操作
ros::ServiceClient   SqlOperationClient;
//点位数据
map<string, string> dictionariesMap_;




//ctrl + c
void mySigintHandler(int sig)
{
    std::cout << "ctrl c  sig: "<<sig<<std::endl;

    ros::shutdown();
    if (ros::isShuttingDown()){
        cout<<"close ros  test.cpp"<<endl;
    }
}


//sql where 条件
void whereSql(std::string Condition,std::string Key_i,std::string Symbol,std::string Value,contnav_srvs::TABLE_CONTORLLER &tableContorllerService){

    //1. where 条件
    contnav_msgs::Dictionaries dictionaries;
    dictionaries.Condition=Condition;
    dictionaries.Key_i=Key_i;
    dictionaries.Symbol=Symbol;
    dictionaries.Value=Value;
    tableContorllerService.request.request_where_dictionariesVector.DictionariesVectors.push_back(dictionaries);

}


//封装条件数据
void mapTOcontnav_srvs(map<string, string> &dictionarieMap_request_value,contnav_srvs::TABLE_CONTORLLER &tableContorllerService){

        map<string, string>::iterator iter_value;
        for(iter_value = dictionarieMap_request_value.begin(); iter_value != dictionarieMap_request_value.end(); iter_value++) {
            contnav_msgs::Dictionaries dictionaries;
            dictionaries.Key_i=iter_value->first;
            dictionaries.Value=iter_value->second;
          //  cout << " =========type==\"value\" || type==\"all\"======="  << endl;

            cout << iter_value->first << " : " << iter_value->second << endl;
            tableContorllerService.request.request_value_dictionariesVector.DictionariesVectors.push_back(dictionaries);
        }

}


/**
TypeId：  0：工作点 1.普通点 ，3：电梯外 ，4：电梯内 ，7：闸机点 ，8：电梯等待点，11：充电桩点，20：狭窄区等待点，76：减速点 ,77: 覆盖点 ,78:重定位点
Status: 0: 空闲 1：正在前往 2：正在此点位上
CommandStatus：-1：不启用 1：启用
*/

//查询点位数据 查询所有的数据


void queryAllFromTABLE_POINT_POSE(){
    try {
        cout << "======******* queryAllFromTABLE_POINT_POSE *******====="<<endl;

        //查询点位数据
        SqlOperationClient.waitForExistence();
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        /**
         * 打开注释为按条件查询

        //1. where 条件
         whereSql("","Id","=","1",tableContorllerService);

        */

        //发送seervice
        SqlOperationClient.call(tableContorllerService);

      //  cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;
        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
            for (int l = 0; l < tableContorllerService.response.result_table_point_poses.size(); ++l) {


            contnav_msgs::TABLE_POINT_POSE table_point_pose =tableContorllerService.response.result_table_point_poses[l];
            cout <<"Id: " <<table_point_pose.Id             <<", ";
            cout <<"MarkName: " <<table_point_pose.MarkName       <<", ";
            cout <<"TypeId: " <<table_point_pose.TypeId         <<", ";
            cout <<"MarkNum: " <<table_point_pose.MarkNum        <<", ";
            cout <<"MapName: " <<table_point_pose.MapName        <<", ";
            cout <<"Mapfloor: " <<table_point_pose.Mapfloor       <<", ";
            cout <<"FieldNumber: " <<table_point_pose.FieldNumber    <<", ";
            cout <<"FieldName: " <<table_point_pose.FieldName      <<", ";
            cout <<"X: " <<table_point_pose.X              <<", ";
            cout <<"Y: " <<table_point_pose.Y              <<", ";
            cout <<"Z: " <<table_point_pose.Z              <<", ";
            cout <<"QX: " <<table_point_pose.QX             <<", ";
            cout <<"QY: " <<table_point_pose.QY             <<", ";
            cout <<"QZ: " <<table_point_pose.QZ             <<", ";
            cout <<"QW: " <<table_point_pose.QW             <<", ";
            cout <<"YAW: " <<table_point_pose.YAW            <<", ";
            cout <<"Status: " <<table_point_pose.Status         <<", ";
            cout <<"Command: " <<table_point_pose.Command        <<", ";
            cout <<"CommandStatus: " <<table_point_pose.CommandStatus  <<"; "<<endl;

        }

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}

void operationFromTABLE_POINT_POSE_bySql(){
    try {

        //操作点位数据
        //   insert into TABLE_DICTIONARES (TargetName,Key_i,Value,Status)  values('场地编号','fieldNumber','00000','0');
        //    UPDATE TABLE_DICTIONARES SET  TargetName = '场地编号',Key_i = ''fieldNumber, Value = '00000', Status = '0'  WHERE Id = %d ;
        //    INSERT INTO TABLE_DICTIONARES (column1,column2,column3,...) VALUES (value1,value2,value3,...);
        //    delete from TABLE_DICTIONARES  WHERE Id = %d
        //    select * from  TABLE_DICTIONARES  ORDER BY Id
        SqlOperationClient.waitForExistence();
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;

        //查询
      //  tableContorllerService.request.sqlStr="select * from  TABLE_POINT_POSE WHERE  MarkName='充电桩' ORDER BY Id";
        tableContorllerService.request.sqlStr=TabeOperationType::select+ " * "+TabeOperationType::from+TabeName::TABLE_POINT_POSE+TabeOperationType::WHERE +TABLE_POINT_POSE_SQL_COLUMN_FIELD::MarkName+"='充电桩'"+TabeOperationType::ORDER+TabeOperationType::BY+TABLE_POINT_POSE_SQL_COLUMN_FIELD::Id;
        tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //增加

        // tableContorllerService.request.sqlStr="insert into TABLE_DICTIONARES (TargetName,Key_i,Value,Status)  values('场地编号33','fieldNumber33','00033','0')";
        // tableContorllerService.request.request_type=TabeOperationType::insertSQLTABL;//insertSQLTABL：增加数据操作

        //更改
        // tableContorllerService.request.sqlStr="UPDATE TABLE_DICTIONARES SET  TargetName = '场地编号666',Key_i = 'fieldNumber66', Value = '00000', Status = '0'  WHERE Id = 16 ";
        // tableContorllerService.request.request_type=TabeOperationType::updateSQLTABL;//updateSQLTABL：更改数据操作

        //删除
        //   tableContorllerService.request.sqlStr="delete from TABLE_DICTIONARES  WHERE Id = 16";
        //  tableContorllerService.request.request_type=TabeOperationType::deleteSQLTABL;//deleteSQLTABL：删除数据操作

        //要操作的数据库表
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;
        SqlOperationClient.call(tableContorllerService);


     //   cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
        for (int l = 0; l < tableContorllerService.response.result_table_point_poses.size(); ++l) {


            contnav_msgs::TABLE_POINT_POSE table_point_pose =tableContorllerService.response.result_table_point_poses[l];
            cout <<"Id: " <<table_point_pose.Id             <<", ";
            cout <<"MarkName: " <<table_point_pose.MarkName       <<", ";
            cout <<"TypeId: " <<table_point_pose.TypeId         <<", ";
            cout <<"MarkNum: " <<table_point_pose.MarkNum        <<", ";
            cout <<"MapName: " <<table_point_pose.MapName        <<", ";
            cout <<"Mapfloor: " <<table_point_pose.Mapfloor       <<", ";
            cout <<"FieldNumber: " <<table_point_pose.FieldNumber    <<", ";
            cout <<"FieldName: " <<table_point_pose.FieldName      <<", ";
            cout <<"X: " <<table_point_pose.X              <<", ";
            cout <<"Y: " <<table_point_pose.Y              <<", ";
            cout <<"Z: " <<table_point_pose.Z              <<", ";
            cout <<"QX: " <<table_point_pose.QX             <<", ";
            cout <<"QY: " <<table_point_pose.QY             <<", ";
            cout <<"QZ: " <<table_point_pose.QZ             <<", ";
            cout <<"QW: " <<table_point_pose.QW             <<", ";
            cout <<"YAW: " <<table_point_pose.YAW            <<", ";
            cout <<"Status: " <<table_point_pose.Status         <<", ";
            cout <<"Command: " <<table_point_pose.Command        <<", ";
            cout <<"CommandStatus: " <<table_point_pose.CommandStatus  <<"; "<<endl;


        }



    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//查询点位数据
void queryFromTABLE_POINT_POSEById(std::string id){
    try {

        cout << "======******* queryFromTABLE_POINT_POSEById *******====="<<endl;

        //查询字典数据
        SqlOperationClient.waitForExistence();
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句

        //2.封装数据
        //1. where 条件

        whereSql("",TABLE_POINT_POSE_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);
        cout << "======*******=================request_where_dictionariesVector.DictionariesVectors.size()  ====="<<tableContorllerService.request.request_where_dictionariesVector.DictionariesVectors.size() <<endl;

        //发送seervice
        SqlOperationClient.call(tableContorllerService);

     //   cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
        for (int l = 0; l < tableContorllerService.response.result_table_point_poses.size(); ++l) {


            contnav_msgs::TABLE_POINT_POSE table_point_pose =tableContorllerService.response.result_table_point_poses[l];

            cout <<"Id: " <<table_point_pose.Id             <<", ";
            cout <<"MarkName: " <<table_point_pose.MarkName       <<", ";
            cout <<"TypeId: " <<table_point_pose.TypeId         <<", ";
            cout <<"MarkNum: " <<table_point_pose.MarkNum        <<", ";
            cout <<"MapName: " <<table_point_pose.MapName        <<", ";
            cout <<"Mapfloor: " <<table_point_pose.Mapfloor       <<", ";
            cout <<"FieldNumber: " <<table_point_pose.FieldNumber    <<", ";
            cout <<"FieldName: " <<table_point_pose.FieldName      <<", ";
            cout <<"X: " <<table_point_pose.X              <<", ";
            cout <<"Y: " <<table_point_pose.Y              <<", ";
            cout <<"Z: " <<table_point_pose.Z              <<", ";
            cout <<"QX: " <<table_point_pose.QX             <<", ";
            cout <<"QY: " <<table_point_pose.QY             <<", ";
            cout <<"QZ: " <<table_point_pose.QZ             <<", ";
            cout <<"QW: " <<table_point_pose.QW             <<", ";
            cout <<"YAW: " <<table_point_pose.YAW            <<", ";
            cout <<"Status: " <<table_point_pose.Status         <<", ";
            cout <<"Command: " <<table_point_pose.Command        <<", ";
            cout <<"CommandStatus: " <<table_point_pose.CommandStatus  <<"; "<<endl;



        }

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//新增点位数据
void insertTABLE_POINT_POSE(){
    try {
        //查询点位数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::insertSQLTABL;//insertSQLTABL：新增数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //2.封装数据
        //新增的值
        map<string, string> dictionarieMap_request_value;
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MarkName]="设备部111";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::TypeId]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MarkNum]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MapName]="map";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Mapfloor]="1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::FieldNumber]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::FieldName]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::X]="1.1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Y]="1.2";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Z]="1.3";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QX]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QY]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QZ]="0.09";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QW]="0.1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::YAW]="0.3";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Status]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Command]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::CommandStatus]="-1";

        mapTOcontnav_srvs(dictionarieMap_request_value,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
      //  cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//修改点位数据
void updateFromTABLE_POINT_POSE(std::string id){
    try {

        //查询点位数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::updateSQLTABL;//updateSQLTABL：更新数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句

        //1. where 条件
        whereSql("",TABLE_POINT_POSE_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);
       // whereSql("AND","TypeId","=","0",tableContorllerService);
        //whereSql("OR","Status","=","0",tableContorllerService);
       // whereSql("AND","MarkName","=","设备部更改3",tableContorllerService);
        //2.封装数据
        //修改的值
        map<string, string> dictionarieMap_request_value;
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MarkName]="设备部111";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::TypeId]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MarkNum]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::MapName]="map";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Mapfloor]="1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::FieldNumber]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::FieldName]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::X]="1.1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Y]="1.2";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Z]="1.3";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QX]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QY]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QZ]="0.09";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::QW]="0.1";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::YAW]="0.3";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Status]="0";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::Command]="EMPTY";
        dictionarieMap_request_value[TABLE_POINT_POSE_SQL_COLUMN_FIELD::CommandStatus]="-1";

        mapTOcontnav_srvs(dictionarieMap_request_value,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);

    //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}
//删除点位数据
void deleteFromTABLE_POINT_POSEById(std::string id){
    try {
        //查询点位数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::deleteSQLTABL;//deleteSQLTABL：删除数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_POINT_POSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句

        //2.封装数据
        //1. where 条件
        whereSql("",TABLE_POINT_POSE_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
     //   cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr<< endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


// rosrun contnav testMysql_node
int main(int argc,char *argv[]){
    //节点名
    string nodeName = "testMysql_contnav_contnav_global_planner_node";
    //初始化节点
    ros::init(argc,argv,nodeName);
    //创建节点
    ros::NodeHandle node;
    //监控 ctrl c
    signal(SIGINT, mySigintHandler);


    //数据库操作
    SqlOperationClient = node.serviceClient<contnav_srvs::TABLE_CONTORLLER::Request,contnav_srvs::TABLE_CONTORLLER::Response>("/SqlOperation");





    //新增点位数据
   // insertTABLE_POINT_POSE();

    //修改点位数据
  //  updateFromTABLE_POINT_POSE("3");

    //删除点位数据
   // deleteFromTABLE_POINT_POSEById("6");

    //查询点位数据 查询所有的数据
     queryAllFromTABLE_POINT_POSE();
     // operationFromTABLE_POINT_POSE_bySql();
    //  queryFromTABLE_POINT_POSEById("1");

    //事件轮询
    ros::spin();
    return 0;
}



